package tools;

import java.awt.Point;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Very important code! Do not delete!
 * @author Luan
 */
public class ConShip {

    public static final String str = "\"name\": ",
                               nomes [] = new String[] {"tipo", "local", "codigo", "estado"};
    public static final int    opcoes[] = new int[]    {2, 2, 0, 1}; //0 - ignorar, 1 - num, 2 - vetor
    public static final Point[] trocas = new Point[] {new Point(2, 4), new Point(3, 2), new Point(4, 3)};
    
    private static final HashMap<Modelo, Integer> map = new HashMap<>();
    private static class Modelo {
        int[] locais, tipos;
        int ce;

        public Modelo(int[] locais, int[] tipos, int ce) {
            this.locais = locais;
            this.tipos = tipos;
            this.ce = ce;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            
            final Modelo other = (Modelo) obj;
            if (!Arrays.equals(this.locais, other.locais))
                return false;
            if (!Arrays.equals(this.tipos, other.tipos))
                return false;
            if (this.ce != other.ce)
                return false;
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 67 * hash + Arrays.hashCode(this.locais);
            hash = 67 * hash + Arrays.hashCode(this.tipos);
            hash = 67 * hash + this.ce;
            return hash;
        }
    }
    
    public static void main(String[] args) {
        try (BufferedReader r = new BufferedReader(new FileReader(new File("ships.json")))) {
            String l, text = "";
            while ((l = r.readLine()) != null)
                text += l;

            int codigoGeral = 0;
            try (PrintWriter p = new PrintWriter(new FileWriter(new File("out.json")))) {
                int j = 0, pre = 0;
                for (int i = 0; i < text.length() - str.length(); i++) { //percorrer o text
                    if (text.substring(i, i + str.length()).equals(str)) {
                        int k = i;
                        for (;;)
                            if (text.charAt(i + str.length() + 2) == '"')
                                break;
                            else
                                i++;
                        String value = text.substring(k + str.length() + 1, i + str.length() + 2);
                        p.print(text.substring(pre, k)); //escrever até então
                        pre = i + str.length() + 4;

                        int[][] values = new int[nomes.length][];
                        int n = 1;
                        for (int m = 0; m < values.length; m++) {
                            int pren = n;
                            while (n < value.length() && Character.isDigit(value.charAt(n)))
                                n++;
                            values[m] = new int[n - pren];
                            for (int o = pren; o < n; o++)
                                values[m][o - pren] = Integer.parseInt(String.valueOf(value.charAt(o)));
                            n++;
                        }

                        //"name": "T012B2C0001S0"
                        //"tipo": [0, 1, 2], "local":[2], "codigo":1, "estado":0
                        String t = "";
                        int cg = codigoGeral++;
                        for (int m = 0; m < values.length; m++)
                            if (opcoes[m] == 2) {
                                if (m == 1) //precisa realizar as trocas dos locais
                                    values[m][0] = troca(values[m][0]);
                                t += " \"" + nomes[m] + "\": [" + values[m][0];
                                for (int o = 1; o < values[m].length; o++) {
                                    if (m == 1) //precisa realizar as trocas dos locais
                                        values[m][o] = troca(values[m][o]);
                                    t += ", " + values[m][o];
                                }
                                t += "],";
                            } else if (opcoes[m] == 1) {
                                String num = "";
                                for (int o = 0; o < values[m].length; o++)
                                    num += values[m][o];
                                t += " \"" + nomes[m] + "\": " + Integer.parseInt(num) + ",";
                            } else { //0, é o código específico
                                String num = "";
                                for (int o = 0; o < values[m].length; o++)
                                    num += values[m][o];                                
                                int ce = Integer.parseInt(num);
                                Modelo modelo = new Modelo(values[0], values[1], ce);
                                if (map.containsKey(modelo)) {
                                    cg = map.get(modelo);
                                    codigoGeral--;
                                } else
                                    map.put(modelo, cg);
                            }
                        p.print("\"codigo\": " + cg + ", \"nome\": \"" + value + "\"," + t);
                        j = i;
                    }
                }
                p.println(text.substring(j + str.length() + 4, text.length()));
            }
        } catch (EOFException ex) {
        } catch (IOException ex) {
            System.err.println(ex.getLocalizedMessage());
        }
    }
    
    private static int troca(int o) {
        for (int q = 0; q < trocas.length; q++)
            if (trocas[q].x == o)
                return trocas[q].y;
        return o;
    }
}
